#include "stdafx.h"
#include "FavoritesAddress.h"

#include <atlbase.h>

uint32 CFavoritesAddress::ShowFavoritesCount = 10;
uint32 CFavoritesAddress::MaxFavoritesCount = 30;
CFavoritesAddress::VectorAddress CFavoritesAddress::m_vecAddress;
CFavoritesAddress::VectorAddress CFavoritesAddress::m_vecAddressShow;

void CFavoritesAddress::AddAddress(const CString& strAddress)
{
	if(strAddress.IsEmpty() || !::PathFileExists(strAddress))
		return;

	size_t i = 0;
	for(; i < m_vecAddress.size(); i++)
	{
		SAddressInfo& addr = m_vecAddress[i];
		if(addr.strAddress == strAddress)
		{
			addr.nUsedCount++;
			break;
		}
	}

	if(i == m_vecAddress.size())
	{
		if(i >= MaxFavoritesCount)
			m_vecAddress.pop_back();

		m_vecAddress.push_back(SAddressInfo());
		SAddressInfo& addr = m_vecAddress.back();
		addr.strAddress = strAddress;
		addr.nUsedCount = 1;
	}
	
	std::sort(m_vecAddress.begin(), m_vecAddress.end(), great_by_used_count());
}

const CFavoritesAddress::VectorAddress& CFavoritesAddress::GetAddress()
{
	m_vecAddressShow.clear();

	for(size_t i = 0; i < ShowFavoritesCount && i < m_vecAddress.size(); i++)
		m_vecAddressShow.push_back(m_vecAddress[i]);

	return m_vecAddressShow;
}
